﻿using MeetAI.Model.DTO;
using MeetAI.Service.IService;
using MeetAI.Utility;
using Nancy;
using Nancy.ModelBinding;

namespace MeetAI.API.Modules
{
    public class articleModule : NancyModule
    {
        private readonly IDy_Article_Service IDy_Article_Service;
        public articleModule(IDy_Article_Service IDy_Article_Service) : base("/v1/article")
        {
            this.IDy_Article_Service = IDy_Article_Service;
            //获取首页列表
            Get["/index/list", true] = async (param, token) =>
            {
                var dto = this.Bind<dy_article_dto>();
                Result res = new Result();
                dto.order = "orders desc";
                res.data = await IDy_Article_Service.getIndexArticleAsync(dto);
                return Response.AsJson(res);
            };
            //获取列表
            Get["/list", true] = async (param, token) =>
             {
                 var dto = this.Bind<dy_article_dto>();
                 Result res = new Result();
                 if (dto == null || !dto.pid.HasValue)
                 {
                     res.code = ResultType.fail;
                     res.message = "参数错误";
                     return Response.AsJson(res);
                 }
                 var lst = await IDy_Article_Service.getArticleAsync(dto);
                 res.data = new
                 {
                     rows = lst,
                     total = lst.TotalCount,
                     pages = lst.TotalPages,
                     hasNext = lst.HasNextPage,
                     hasPrev = lst.HasPreviousPage
                 };
                 return Response.AsJson(res);
             };
            //获取明细
            Get["/detail", true] = async (param, token) =>
            {
                Result res = new Result();
                int? id = Request.Query.id;
                if (!id.HasValue)
                {
                    res.code = ResultType.fail;
                    res.message = "参数错误";
                    return Response.AsJson(res);
                }
                res.data = await IDy_Article_Service.FindAsync(s => s.id.Equals(id));
                return Response.AsJson(res);
            };
        }
    }
}